/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.service.loader.internal;

import lombok.NoArgsConstructor;
import org.jrose.api.annotation.common.ConfigurationLocation;
import org.jrose.api.annotation.remote.ExposeHttpService;
import org.jrose.core.util.CollectionUtils;
import org.jrose.entity.config.InitialAttribute;
import org.jrose.service.loader.AbstractStandardConfigurationLoader;
import org.jrose.service.loader.InitialAttributeLoader;
import org.w3c.dom.Element;

import javax.inject.Named;
import java.util.List;

import static org.jrose.core.util.ConversionUtils.toInt;
import static org.jrose.core.util.XMLUtils.getChildElementText;

/**
 * Default {@link InitialAttributeLoader} implementation.
 *
 * @author Xiaohai Zhang
 * @since 2013-08-31 21:28
 */
@Named
@NoArgsConstructor
@ConfigurationLocation("/config/initial-attribute-configuration.xml")
@AbstractStandardConfigurationLoader.StandardConfiguration("initial-attribute")
@ExposeHttpService(name = "/initialAttributeLoader", serviceInterface = InitialAttributeLoader.class)
public class InitialAttributeLoaderImpl extends AbstractStandardConfigurationLoader<InitialAttribute> implements InitialAttributeLoader {
    private InitialAttribute initialAttribute;

    @Override
    protected InitialAttribute parseElement(Element element) {
        InitialAttribute initialAttribute = new InitialAttribute();
        initialAttribute.setHiMaxHealth(toInt(getChildElementText(element, "hi-max-health")));
        initialAttribute.setLoMaxHealth(toInt(getChildElementText(element, "lo-max-health")));
        initialAttribute.setHiMaxMana(toInt(getChildElementText(element, "hi-max-mana")));
        initialAttribute.setLoMaxMana(toInt(getChildElementText(element, "lo-max-mana")));
        initialAttribute.setHiAttack(toInt(getChildElementText(element, "hi-attack")));
        initialAttribute.setLoAttack(toInt(getChildElementText(element, "lo-attack")));
        initialAttribute.setHiDefense(toInt(getChildElementText(element, "hi-defense")));
        initialAttribute.setLoDefense(toInt(getChildElementText(element, "lo-defense")));
        initialAttribute.setHiSpiritAttack(toInt(getChildElementText(element, "hi-spirit-attack")));
        initialAttribute.setLoSpiritAttack(toInt(getChildElementText(element, "lo-spirit-attack")));
        initialAttribute.setHiSpiritDefense(toInt(getChildElementText(element, "hi-spirit-defense")));
        initialAttribute.setLoSpiritDefense(toInt(getChildElementText(element, "lo-spirit-defense")));
        initialAttribute.setHiSpeed(toInt(getChildElementText(element, "hi-speed")));
        initialAttribute.setLoSpeed(toInt(getChildElementText(element, "lo-speed")));
        return initialAttribute;
    }

    @Override
    protected void processConfigurations(List<InitialAttribute> configurations) {
        initialAttribute = CollectionUtils.firstElement(configurations);
    }

    @Override
    public InitialAttribute load() {
        initializeLoader();
        return initialAttribute;
    }
}
